const db = require("../../config/db");

module.exports = {
    //关注
    addFollow({ user_id, follow_user, follow_name, follow_avators }) {
        return new Promise((resolve, reject) => {
            let check_follow_sql = `select * from db_follow where user_id="${user_id}" and follow_user="${follow_user}"`;
            db.query(check_follow_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        let insert_follow_sql = `insert into db_follow(user_id,follow_user,follow_name,follow_avators) values("${user_id}","${follow_user}","${follow_name}","${follow_avators}")`;
                        db.query(insert_follow_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "关注成功",
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 201,
                            msg: "该用户已经关注",
                        });
                    }
                }
            });
        });
    },

    //取消关注
    removeFollow({ user_id, follow_user }) {
        return new Promise((resovle, reject) => {
            let check_follow_sql = `select * from db_follow where user_id="${user_id}" and follow_user="${follow_user}"`;
            db.query(check_follow_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length >= 1) {
                        let remove_follow_sql = `DELETE FROM db_follow WHERE user_id="${user_id}" and follow_user="${follow_user}"`;
                        db.query(remove_follow_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resovle({
                                    code: 200,
                                    msg: "取消关注",
                                });
                            }
                        });
                    } else {
                        resovle({
                            code: 201,
                            msg: "还未关注该用户",
                        });
                    }
                }
            });
        });
    },

    //获取关注的数量
    getFollowCount({ user_id }) {
        return new Promise((resolve, reject) => {
            let follow_count_sql = `SELECT COUNT(*) FROM db_follow where user_id="${user_id}"`;
            db.query(follow_count_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取粉丝的数量
    getFansCount({ follow_user }) {
        return new Promise((resolve, reject) => {
            let fans_count_sql = `SELECT COUNT(*) FROM db_follow where follow_user="${follow_user}"`;
            db.query(fans_count_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取用户关注列表
    getFollowList({ user_id }) {
        return new Promise((resolve, reject) => {
            let check_follow_sql = `select * from db_follow where user_id="${user_id}"`;
            db.query(check_follow_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取用户粉丝列表
    getFansList({ follow_user }) {
        // console.log("111", follow_user);
        return new Promise((resolve, reject) => {
            let check_fans_sql = `select * from db_follow where follow_user="${follow_user}"`;
            db.query(check_fans_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
};
